package com.zhgxi.zf.action;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slave4j.orm.PageData;
import org.slave4j.utils.WebUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;

import com.zhgxi.zf.entity.TestEty;
import com.zhgxi.zf.service.TestEtyService;

@Controller("TestEtyAction")
public class TestEtyAction {

	@Resource
	private TestEtyService testEtyService;

	public String sayTest(String name) {
		return name + " Hello!";
	}

	public ModelMap list(HttpServletRequest request) {
		PageData<TestEty> pageData = new PageData<TestEty>();
		// 给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		pageData = testEtyService.find(pageData);

		return new ModelMap(pageData);
	}

	public String addForm(Model model) {
		return "/zf/testEty/input";
	}

	public String editForm(@PathVariable("id") Integer id, Model model) {
		TestEty testEty = testEtyService.find(id);
		model.addAttribute(testEty);
		return "/zf/testEty/input";
	}

	public String save(TestEty testEty) {
		if (testEty.isNew()) {
			testEtyService.save(testEty);
		} else {
			testEtyService.update(testEty);
		}

		return "redirect:/zf/testEty/list";
	}

	public String delete(@PathVariable("id") Integer id) {
		testEtyService.delete(id);
		return "redirect:/zf/testEty/list";
	}

	public String visible(@PathVariable("id") Integer id) {
		testEtyService.visible(id);
		return "redirect:/zf/testEty/list";
	}

	public String unVisible(@PathVariable("id") Integer id) {
		testEtyService.unVisible(id);
		return "redirect:/zf/testEty/list";
	}

	@InitBinder
	public void InitBinder(WebDataBinder dataBinder) {
		dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(
				new SimpleDateFormat("yyyy-MM-dd"), false));
	}

}
